<?php

namespace App\Models;

use App\Http\Controllers\Admin\CommonController;
use Exception;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
 * 数字资源推荐阅读表
 */
class ResourceReadRecom extends BaseModel
{
    use HasFactory;

    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'change_time';


    protected $table = 'resource_read_recom';


    /*关联书单书籍*/
    // public function conReadingListShopBook()
    // {
    //     return $this->belongsToMany(ShopBook::class, BookHomeReadingListBook::class, 'reading_id', 'book_id');
    // }


    // /*关联书单书籍*/
    // public function conReadingListLibBook()
    // {
    //     return $this->belongsToMany(LibBookRecom::class, BookHomeReadingListBook::class, 'reading_id', 'book_id');
    // }



    /**
     * 获取列表
     * @param limit int 分页大小
     * @param is_play string 是否发布  1 发布 2 不发布
     * @param keywords string 搜索关键词
     * @param correct_read_id 当前每日荐书的id，如果传了则表示是获取往前荐书  不传获取全部
     */
    public function lists($search_data = [])
    {
        $keywords = $search_data['keywords'] ?? '';
        $is_play = $search_data['is_play'] ?? '';
        $correct_read_id = $search_data['correct_read_id'] ?? '';
        $limit = !empty($search_data['limit']) ? $search_data['limit'] : 10;
        $manage_resource_business_ids = request()->manage_info['resource_business_ids'] ?? [];

        $res = $this->select('id', 'img', 'name', 'is_play', 'intro', 'recommendation', 'create_time')
            ->where(function ($query) use ($keywords) {
                if ($keywords) {
                    $query->where('name', 'like', "%$keywords%");
                }
            })->where(function ($query) use ($is_play) {
                if ($is_play) {
                    $query->where('is_play', $is_play);
                }
            })->where(function ($query) use ($correct_read_id) {
                if ($correct_read_id) {
                    $query->where('id', '<', $correct_read_id); //获取往前推荐
                }
            })->where(function ($query) use ($manage_resource_business_ids) {
                if ($manage_resource_business_ids) {
                    $query->where('manage_id', request()->manage_id);
                }
            })
            ->where('is_del', 1)
            ->orderByDesc('id')
            ->paginate($limit)
            ->toArray();

        return $res;
    }

    /**
     * 详情
     * @param id
     * @param is_play int 发布   1 发布  2 撤销
     */
    public function detail($id, $is_play = null)
    {
        $res = $this->select('id', 'img', 'name', 'is_play', 'intro', 'recommendation', 'create_time')
            ->where('id', $id)
            ->where(function ($query) use ($is_play) {
                if ($is_play) {
                    $query->where('is_play', $is_play);
                }
            })
            ->where('is_del', 1)
            ->first();
        if (empty($res)) {
            return false;
        }
        return $res;
    }

    /**
     * 获取最新一期的数据
     * @param id
     */
    public function getNewReadRecomData()
    {
        $res = $this->select('id', 'img', 'name', 'intro', 'recommendation')
            ->where('is_del', 1)
            ->where('is_play', 1)
            ->orderByDesc('id')
            ->first();
        if (empty($res)) {
            return null;
        }
        $res = $res->toArray();
        $res['name'] = (strpos($res['name'], '【') === false && strpos($res['name'], '】') === false) ? ("【" . $res['name'] . "】") : $res['name']; //判断是否有 【】，没有则加上

        $resourceReadRecomBookModel = new ResourceReadRecomBook();
        $resource_read_recom_book = $resourceReadRecomBookModel->lists(['read_id' => $res['id'], 'limit' => 5]);
        $res['resource_read_recom_book'] = $resource_read_recom_book['data'];
        return $res;
    }
}
